package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.CommentMapper;
import com.tedu.weibo.pojo.dto.CommentDTO;
import com.tedu.weibo.pojo.entity.Comment;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.CommentVO;
import com.tedu.weibo.result.JsonResult;
import com.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
@Api(tags = "评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("/insert")
    @ApiOperation("发表评论")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        System.out.println("开始评论操作...");
        System.out.println(commentDTO);
        //验证登录
        User user = (User) session.getAttribute("user");
        if (user==null){//没登录
            return new JsonResult(Status.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(user.getId());
        commentMapper.insertComment(comment);
        return JsonResult.success();
    }

    @GetMapping("/selectByWeiboId")
    @ApiOperation("查询指定微博评论")
    @ApiImplicitParam(value = "微博id",name = "id",required = true,example = "6",dataType = "int")
    public JsonResult findByWeiboId(Integer id){
        /*System.out.println("开始查询评论...");
        System.out.println("评论id为:"+id);*/
        return JsonResult.success(commentMapper.selectById(id));
    }

    @GetMapping("/selectByWeiboIdAndUserId")
    @ApiOperation("查询指定微博下指定用户的评论")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "微博id",name = "weiboId",required = true,example = "10",dataType = "int"),
            @ApiImplicitParam(value = "用户id",name = "userId",required = true,example = "1",dataType = "int")
    })
    public List<CommentVO> findByWeiboIdAndUserId(Integer weiboId, Integer userId){
        return null;
    }
}
